<?php

/**
 * Pictet2_Settings
 * 
 * Static settings interface.
 * If it can not find the settings.xml file in the .xml folder it will throw an exception.
 */
class Pictet2_Settings {
	
	static private $_instance = null;
	
	static private $_storageClass;
	
	private function __construct() {}
	
	static private function _checkSettings() {
		if(null == self::$_instance) {
			if(file_exists(ROOT_DIR . DS . ".xml" . DS . "settings.xml")) {
				$initial = simplexml_load_file(ROOT_DIR . DS . ".xml" . DS . "settings.xml");
			} else {
				if(!is_dir(ROOT_DIR . DS . ".xml")) {
					mkdir(ROOT_DIR . DS . ".xml");
				}
				$initial = new SimpleXMLElement("<settings><engine>Xml</engine></settings>");
				file_put_contents(ROOT_DIR . DS . ".xml" . DS . "settings.xml", $initial->asXml());
			}
			self::_factory($initial->engine, $initial->params);
		}		
	}
	
	static private function _factory($storageClass, $params) {
		if($storageClass != "Xml" || !$storageClass) {
			$storageClass = "Plugins_Settings_" . $storageClass;
		} else {
			$storageClass = "Pictet2_Settings_Xml";
		}
		self::$_storageClass = $storageClass;
		self::$_instance = call_user_func(array($storageClass, "getInstance"), $params);
	}
	
	static public function getSetting($var) {
		self::_checkSettings();
		return self::$_instance->get($var);
	}
	
	static public function setSettings($var, $val) {
		self::_checkSettings();
		return self::$_instance->set($var, $val);
	}
	
}